﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DrivingCRM.Controller;

namespace DrivingCRM.Views.Private.CRM
{
    public partial class AddMotorcycletoRevision : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Page.IsPostBack == false)
                this.DP_moto.SelectedDate = DateTime.Now;
            else
            {
                if (Session["User"] != null || (Session["User"] as List<string>)[0] == "Client" || (Session["User"] as List<string>)[0] == "Stakeholder")
                {
                    /// si c'est un vendeur il n'a accès qu'au véhicule de son agence
                    if ((Session["User"] as List<string>)[0] == "Salesman")
                    {
                        this.DS_Moto.Where = "it.AgencyIdAgency == @CurrentAgency";
                        this.DS_Moto.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.Int32, Name = "CurrentAgency", DefaultValue = (Session["User"] as List<string>)[2] });
                    }
                }
                else
                {
                    Response.Redirect("~/Views/Default.aspx");
                }
            }
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de mise en révision du véhicule
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void put_Moto_Revision(object sender, EventArgs e)
        {
            string ret = EntityManager.PutInRevision(int.Parse(DDL_Moto.SelectedValue), (DateTime)this.DP_moto.SelectedDate);
            if (ret == "No error")
                this.L_ErrorMessage.Text = "La moto a été mise en révision.";
            else
                ///this.L_ErrorMessage.Text = "An error occurred during the creation of the new event. Please fill the required fields.";
                this.L_ErrorMessage.Text = ret;
        }
    }
}